#!perl -w
use strict;
#use lib '/usr/home/fotios/lib/perl5';

package Includes;
my $WEBROOT = "/usr/local/www";
my $DBPATH = "$WEBROOT/.inc/dbs";

# Log the information to Syslog
sub my_log($$$@){
	my($task,$level,$format,@vals) = @_;
	use Sys::Syslog qw/ :standard /;
	openlog($task,0);
	syslog(1,sprintf("%s: %s",$level,sprintf($format,@vals)));
	closelog();
}

# Connect to a SQLite database
sub connect($){
	use DBI;
	my ($dbfile) = @_;
	my $dbh = DBI->connect("dbi:SQLite:dbname=".join("/",$DBPATH,$dbfile),"","") 
		or die $DBI::errstr;
	return $dbh;
}

# Get the hash file that is used by the secretary
sub get_hash(){
	my $HASHFILE = "$WEBROOT/.inc/hash";
	open(FILE,$HASHFILE) || die("Couldn't open file: $!");
	my $hash = <FILE>;
	chomp($hash);
	close(FILE);
	return $hash;
}

# Set the banner
sub get_banner(){
	use CGI qw/ :standard /;
	return div({id=>'banner',class=>'center'},
			img({src=>'images/banner.png'})
			);
}

sub get_html($){
	use CGI qw/ :standard /;
	return start_html( 
			-title=>sprintf("Do Gooder's R Us - %s",shift), 
			-style=>[
				{	'src'=>'css/style.css'},
				{ 'src'=>'css/forms.css'},
				{ 'src'=>'css/tables.css'}
			],
			-script=>[
				{ 
					-type => 'text/javascript',
					-src => 'js/jquery.js'
				},
				{ 
					-type => 'text/javascript',
					-src => 'js/jquery.corner.js'
				},
				{
					-type	=> 'text/javascript',
					-src	=> 'js/functions.js'
				}
			]
		);
}

sub get_loginfields(){
	use CGI qw/ :standard /;
	my %defaults = (
			-class=>'center',
			);
	return 
			div({class=>'login_fields'},[
					&fields('username'),
					&fields('password','password')
					]
				);
}

sub fields($){
	my ($name,$class,$retval) = (@_);
	my %hash = (
			-name=>$name,
			-value=>ucfirst($name),
			-class=>join(' ','center',"clear")
			);
	use Switch;
	switch($class){
		case /password/ { $retval = password_field({%hash}) }
		else						{ $retval = textfield({%hash}) }
	}
	return $retval
}

1;


